/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "doc_bigtutorial.h"

#include <QTextStream>
#include <QFile>

BigTutorialPart::BigTutorialPart(const QString& title) : IntroducedDocument(title)
{
}

BigTutorialPart::BigTutorialPart(const QDomElement& de)
{
    fromDomElement(de);
}

BigTutorialPart::~BigTutorialPart()
{
    for(QList<MiniTutorial*>::iterator it = chapters.begin();
	it != chapters.end(); ++it)
    {
	delete (*it);
    }
}

QDomElement BigTutorialPart::toDomElement(QDomDocument& doc) const
{
    QDomElement chapter = doc.createElement("chapter");
    QDomElement deTitle = doc.createElement("title");
    QDomElement deProgress = doc.createElement("progress");
    QDomElement deDifficult = doc.createElement("difficult");
    QDomElement deIntro = doc.createElement("intro");
    QDomElement deMinitutos = doc.createElement("minitutos");
    QDomElement deConclu = doc.createElement("conclu");

    deTitle.appendChild(doc.createCDATASection(title));
    deProgress.appendChild(doc.createCDATASection(QString::number(progress)));
    deDifficult.appendChild(doc.createCDATASection(QString::number(difficult)));
    deIntro.appendChild(doc.createCDATASection(intro->getContents() ));
    deConclu.appendChild(doc.createCDATASection(conclu->getContents() ));

    for(QList<MiniTutorial*>::const_iterator it = chapters.begin(); it != chapters.end(); ++it)
	deMinitutos.appendChild( (*it)->toDomElement(doc) );
    
    chapter.appendChild(deTitle);
    chapter.appendChild(deProgress);
    chapter.appendChild(deDifficult);
    chapter.appendChild(deIntro);
    chapter.appendChild(deMinitutos);
    chapter.appendChild(deConclu);

    return chapter;
			
}

void BigTutorialPart::fromDomElement(const QDomElement& de)
{
    QDomElement deTitle = de.firstChildElement();
    QDomElement deProgress = deTitle.nextSiblingElement("progress");
    QDomElement deDifficult = deTitle.nextSiblingElement("difficult");
    QDomElement deIntro = deTitle.nextSiblingElement("intro");
    QDomElement deMinitutos = deTitle.nextSiblingElement("minitutos");
    QDomElement deConclu = deTitle.nextSiblingElement("conclu");

    if(deTitle.tagName() == "title" && !deIntro.isNull() &&
       !deMinitutos.isNull() && !deConclu.isNull() )
    {
		setTitle(deTitle.text());
		setIntro(deIntro.text());
		setConclu(deConclu.text());
                setProgress(deProgress.text().toInt());
                setDifficult(deDifficult.text().toInt());

		QDomNodeList listMinitutos = deMinitutos.childNodes();
		int nbMinitutos = listMinitutos.count();
		if(nbMinitutos > 0)
			for(int i = 0; i < nbMinitutos; ++i)
			chapters.append(new MiniTutorial(listMinitutos.at(i).toElement()));
	}
    else
		throw 300;
}

///////////////////////////////////////////////////////////////////////
QList<MiniTutorial*>& BigTutorialPart::allChapters()
{
    return chapters;
}

///////////////////////////////////////////////////////////////////////
BigTutorial::BigTutorial(const QString& title) : IntroducedDocument(title)
{
}
    
BigTutorial::BigTutorial(const QDomElement& de)
{
    fromDomElement(de);
}

BigTutorial::~BigTutorial()
{
    for(QList<BigTutorialPart*>::iterator it = parts.begin();
	it != parts.end(); ++it)
    {
	delete (*it);
    }
}

QDomElement BigTutorial::toDomElement(QDomDocument& doc) const
{
    QDomElement bigtuto = doc.createElement("bigtuto");
    QDomElement deTitle = doc.createElement("title");
    QDomElement deLicence = doc.createElement("licence");
    QDomElement deProgress = doc.createElement("progress");
    QDomElement deDifficult = doc.createElement("difficult");
    QDomElement deIntro = doc.createElement("intro");
    QDomElement deChapters = doc.createElement("chapters");
    QDomElement deConclu = doc.createElement("conclu");

    deTitle.appendChild(doc.createCDATASection(title));
    deLicence.appendChild(doc.createCDATASection(QString::number(licence)));
    deProgress.appendChild(doc.createCDATASection(QString::number(progress)));
    deDifficult.appendChild(doc.createCDATASection(QString::number(difficult)));
    deIntro.appendChild(doc.createCDATASection(intro->getContents() ));
    deConclu.appendChild(doc.createCDATASection(conclu->getContents() ));

    for(QList<BigTutorialPart*>::const_iterator it = parts.begin(); it != parts.end(); ++it)
	deChapters.appendChild( (*it)->toDomElement(doc) );

    bigtuto.appendChild(deTitle);
    bigtuto.appendChild(deIntro);
    bigtuto.appendChild(deChapters);
    bigtuto.appendChild(deConclu);

    return bigtuto;

}

void BigTutorial::toFile(const QString& fname)
{
    // XML
    QDomDocument doc("Bigtuto");
    QDomElement root = doc.createElement("document");
    QDomElement bigtuto = toDomElement(doc);

    root.setAttribute("type","bigtuto");
    root.appendChild(bigtuto);
    
    doc.appendChild(root);

    // FILE
    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
		QTextStream ts(&file);
		ts << doc.toString();
		file.close();

		filename = fname;
    }
    else
		throw 401;
}

void BigTutorial::fromDomElement(const QDomElement& de)
{
    QDomElement deTitle = de.firstChildElement();
    QDomElement deLicence = deTitle.nextSiblingElement("licence");
    QDomElement deProgress = deTitle.nextSiblingElement("progress");
    QDomElement deDifficult = deTitle.nextSiblingElement("difficult");
    QDomElement deIntro = deTitle.nextSiblingElement("intro");
    QDomElement deChapters = deTitle.nextSiblingElement("chapters");
    QDomElement deConclu = deTitle.nextSiblingElement("conclu");

    if(deTitle.tagName() == "title" && !deIntro.isNull() &&
       !deChapters.isNull() && !deConclu.isNull() )
    {
		setTitle(deTitle.text());
		setIntro(deIntro.text());
		setConclu(deConclu.text());
                setLicence(deLicence.text().toInt());
                setProgress(deProgress.text().toInt());
                setDifficult(deDifficult.text().toInt());

		QDomNodeList listChapters = deChapters.childNodes();
		int nbChapters = listChapters.count();
		if(nbChapters > 0)
			for(int i = 0; i < nbChapters; ++i)
			parts.append(new BigTutorialPart(listChapters.at(i).toElement()));
    }
    else
		throw 300;
}

void BigTutorial::fromFile(const QString& fname)
{
    QDomDocument doc("Bigtuto");
    QFile file(fname);
    if( file.open(QIODevice::ReadOnly) )
    {
		if( doc.setContent(&file) )
		{
			file.close();
			
			QDomElement root = doc.documentElement();
			if(root.tagName() != "document")
				throw 300;
			
			if(root.firstChildElement().tagName() != "bigtuto")
				throw 300;

			filename = fname;
			fromDomElement(root.firstChildElement());
		}
		else
		{
			file.close();
			throw 301;
		}
    }
    else
		throw 400;
}

QList<BigTutorialPart*>& BigTutorial::allParts()
{
    return parts;
}
